<?php
/**
 * 导入角色
 * @dinglin
 */
apf_require_class('Oa_Job_Abstract');
apf_require_class('Bll_Hr_OutInterface');
apf_require_class('Const_BaseInfo');

class Oa_Job_Flow_InputRole extends Oa_Job_Abstract {

    private $read_db = "hroa_slave";//读数据库
    private $writedb = "hroa_master";//写数据库
    private $no_input ;//没有导入的数据
    public function run(){
        $this->log("用户角色导入开始 ".date('Y-m-d H:i:s'));
        $citys = $this->_get_citys();
        $temp = $this->_get_key_value($citys, 'city_id');
        $city_ids = array_keys($temp);
        //招聘
        $data = $this->_data_recruit();
        $this->_execute_role_data($citys,$data,2,$city_ids);
        sleep(1);
        //hr
        $data = $this->_data_hr();
        $this->_execute_role_data($citys,$data,5,$city_ids);
        sleep(1);
        //hrd
        $data = $this->_data_hrd();
        $this->_execute_role_data($citys,$data,6,$city_ids);
        //hrd具有所有hr权限
        sleep(1);
        $data = $this->_data_hrd();
        $this->_execute_role_data($citys,$data,5,$city_ids);
        sleep(1);
        //行政
        $data = $this->_data_admin();
        $this->_execute_role_data($citys,$data,3,$city_ids);
        sleep(1);
        //财务
        $data = $this->_data_fin();
        $this->_execute_role_data($citys,$data,7,$city_ids);
        sleep(1);
        //it
        $data = $this->_data_it();
        $this->_execute_role_data($citys,$data,4,$city_ids);
        sleep(1);
        //图书管理
        $data = $this->_data_book();
        $this->_execute_role_data($citys,$data,8,$city_ids);
        $this->log("用户角色导入结束 ".date('Y-m-d H:i:s'));
        //没导入角色
        if($this->no_input){
            foreach ($this->no_input as $val){
                $this->log("没导入角色 ：".join('，',$val));
            }
        }
        $this->_destroy($temp);
    }
    /**
     * 清理表
     */
    public function clear_table(){
        $sql = "DELETE FROM role ";
        $this->execute( $sql,array(), $this->writedb);
    }
    /**
     * 处理
     */
    private function _execute_role_data(&$citys,&$data,$type,$city_ids){
        $user_codes = $this->_get_user_code($data);
        $users_info = $this->_get_user_info($user_codes);
        foreach ($data as $val){
            if(!$users_info[$val[1]]){
                $this->no_input[] = array($val[0],$val[1],$this->_role_name($type));
            }else{
                if($val[0]=="全国"){
                    foreach ($city_ids as $city_id){
                        $this->_insert_role($city_id,$users_info[$val[1]],$type);
                    }
                }else{
                    $this->_insert_role($citys[$val[0]]['city_id'],$users_info[$val[1]],$type);
                }
            }
        }
        $this->_destroy($data);
        $this->_destroy($user_codes);
        $this->_destroy($users_info);
    }
    /**
     * 插入角色
     */
    private function _insert_role($city_id,&$user,$type){
        $sql = "INSERT INTO role(user_id,user_info,city_id,role_type,last_update) VALUES(";
        $sql.= $user['user_id'].',';
        $sql.= "'".$user['real_name']." - ".$user['position_name']."',";
        $sql.= $city_id.',';
        $sql.= $type.',';
        $sql.= "'".date('Y-m-d H:i:s')."')";
        $this->execute( $sql,array(), $this->writedb);
    }
    //角色
    private function _role_name($type){
        $res = array(
                1=>'普通员工',
                2=>'招聘',
                3=>'行政',
                4=>'IT',
                5=>'HR',
                6=>'HRD',
                7=>'财务',
                8=>'图书',
                9=>'主管'
        );
        return $res[$type];
    }
    /**
     * 获取data中的user_code
     */
    private function _get_user_code($data){
        $result = array();
        foreach($data as $val){
            $result[] = $val[1];
        }
        $this->_destroy($data);
        return array_unique($result);
    }
    /**
     * 根据工号查询用户信息
     */
    private function _get_user_info($user_codes){
        //user name
        $sql = "SELECT real_name,user_id,user_code FROM user where user_code in('".join("','",$user_codes)."')";
        $user_datas= $this->get_db_rows($sql,$this->read_db,array());
        $result = $this->_get_key_value($user_datas, "user_code");
        $this->_destroy($user_datas);
        //position
        $temp = $this->_get_key_value($result, "user_id");
        $user_ids = array_keys($temp);
        $user_ids = array_unique($user_ids);
        $this->_destroy($temp);
        $sql = "SELECT user_id,job_id FROM work WHERE is_parttime=1 and user_id in(".join(',',$user_ids).")";
        $works= $this->get_db_rows($sql,$this->read_db,array());
        $this->_destroy($user_ids);
        $user_works = $this->_get_key_value($works, "user_id");
        $temp = $this->_get_key_value($works, "job_id");
        $job_ids = array_keys($temp);
        $job_ids = array_unique($job_ids);
        $this->_destroy($temp);
        $this->_destroy($works);
        $sql = "SELECT job_id,position_name FROM job WHERE job_id in(".join(',',$job_ids).")";
        $jobs= $this->get_db_rows($sql,$this->read_db,array());
        $user_jobs = $this->_get_key_value($jobs, "job_id");
        $this->_destroy($jobs);
        //merege
        foreach($result as $index => $user){
            $result[$index]['position_name'] = $user_jobs[$user_works[$user['user_id']]['job_id']]['position_name'];
        }
        return $result;
    }
    /**
     * 所有城市
     */
    private function _get_citys(){
        $bll = new Bll_Hr_OutInterface();
        $citys = $bll->get_city_list(false);
        return $this->_get_key_value($citys, "city_name");
    }
    /**
     * 释放内存
     * @param unknown_type $data
     */
    private function _destroy(&$data){
        $data = null;
        unset($data);
    }
    /**
     * key -> value
     */
    private function _get_key_value($user_datas,$index_filed){
        $result = array();
        foreach($user_datas as $val){
            $result[$val[$index_filed]] = $val;
        }
        $this->_destroy($user_datas);
        return $result;
    }
    /**
     * 行政招聘
     */
    private function _data_recruit(){
        return array(
                array('全国','SHC462'),
                array('全国','SHD616'),
                array('全国','SHS059'),
                array('全国','SHD1139'),
                array('全国','SHF3893'),
                array('全国','SHF3916'),
                array('全国','SHF4192'),
                array('三亚','SYAE2757'),
                array('三亚','SZC472'),
                array('深圳','SZC472'),
                array('惠州','SZC472'),
                array('深圳','SZF3078'),
                array('惠州','SZF3078'),
                array('广州','GZE1840'),
                array('广州','GZE1963'),
                array('重庆','CQF3292'),
                array('成都','CDB188'),
                array('成都','CDE1611'),
                array('南京','NJB195'),
                array('南京','NJF3902'),
                array('青岛','QDD1198'),
                array('北京','BJE1374'),
                array('北京','BJF4039'),
                array('杭州','HZE1458'),
                array('合肥','HFF3118'),
                array('合肥','NJB195'),
                array('石家庄','SJZE2158'),
                array('石家庄','TJE1748'),
                array('苏州','SUZD1265'),
                array('无锡','SUZD1265'),
                array('昆山','SUZD1265'),
                array('苏州','NJB195'),
                array('无锡','NJB195'),
                array('昆山','NJB195'),
                array('天津','TJE1748'),
                array('天津','TJD969'),
                array('郑州','ZZF3222'),
                array('东莞','GZE1839'),
                array('佛山','GZE1839'),
                array('佛山','GZE1840'),
                array('东莞','GZE1840'),
                array('武汉','WHC519'),
                array('南昌','NCF3066'),
                array('大连','DLF3348'),
                array('大连','TJE1748'),
                array('济南','JND662'),
                array('沈阳','SYF4390'),
                array('沈阳','TJE1748'),
                array('长春','CCE2531'),
                array('长春','TJE1748'),
                array('长沙','CSD1163'),
                array('长沙','GZE1840'),
                array('厦门','XMF3977'),
                array('西安','XAD1199'),
                array('昆明','KME2121'),
                array('福州','FZF4161'),
                array('福州','GZE1840'),
                array('宁波','NBE1860'),
                array('太原','BJE2132'),
                array('太原','TJE1748')
                );
    }
    /**
     * hr
     */
    private function _data_hr(){
        return array(
                array('全国','SHD823'),
                array('全国','SHD1142'),
                array('全国','SHD1153'),
                array('全国','SHE2192'),
                array('三亚','SYAE2757'),
                array('三亚','SZC472'),
                array('深圳','SZC472'),
                array('惠州','SZC472'),
                array('深圳','SZF3078'),
                array('惠州','SZF3078'),
                array('广州','GZE1840'),
                array('广州','GZE1963'),
                array('重庆','CQF3292'),
                array('成都','CDB188'),
                array('成都','CDE1611'),
                array('南京','NJB195'),
                array('南京','NJF3902'),
                array('青岛','QDD1198'),
                array('北京','BJE1374'),
                array('北京','BJF3194'),
                array('杭州','HZE1458'),
                array('合肥','HFF3118'),
                array('合肥','NJB195'),
                array('石家庄','SJZE2158'),
                array('石家庄','TJE1748'),
                array('苏州','SUZD1265'),
                array('无锡','SUZD1265'),
                array('昆山','SUZD1265'),
                array('苏州','NJB195'),
                array('无锡','NJB195'),
                array('昆山','NJB195'),
                array('天津','TJE1748'),
                array('天津','TJD969'),
                array('郑州','ZZF3222'),
                array('佛山','GZE1839'),
                array('东莞','GZE1839'),
                array('佛山','GZE1840'),
                array('东莞','GZE1840'),
                array('武汉','WHC519'),
                array('南昌','NCF3066'),
                array('大连','DLF3348'),
                array('大连','TJE1748'),
                array('济南','JND662'),
                array('沈阳','SYF4390'),
                array('沈阳','TJE1748'),
                array('长春','CCE2531'),
                array('长春','TJE1748'),
                array('长沙','CSD1163'),
                array('长沙','GZE1840'),
                array('厦门','XMF3977'),
                array('西安','XAD1199'),
                array('昆明','KME2121'),
                array('福州','FZF4161'),
                array('福州','GZE1840'),
                array('宁波','NBE1860'),
                array('太原','BJE2132'),
                array('太原','TJE1748'),
                );
    }
    /**
     * 行政
     */
    private function _data_admin(){
        return array(
                array('上海','SHA044'),
                array('上海','SHF4015'),
                array('上海','SHF4300'),
                array('上海','SHE1627'),
                array('三亚','SYAE2757'),
                array('三亚','SZC472'),
                array('深圳','SZC472'),
                array('惠州','SZC472'),
                array('深圳','SZF3078'),
                array('惠州','SZF3078'),
                array('广州','GZE1840'),
                array('广州','GZE1963'),
                array('重庆','CQF3292'),
                array('成都','CDB188'),
                array('成都','CDE1611'),
                array('南京','NJB195'),
                array('南京','NJF3902'),
                array('青岛','QDD1198'),
                array('北京','BJE1374'),
                array('北京','BJF4095'),
                array('杭州','HZE1458'),
                array('合肥','HFF3118'),
                array('合肥','NJB195'),
                array('石家庄','SJZE2158'),
                array('石家庄','TJE1748'),
                array('苏州','SUZD1265'),
                array('无锡','SUZD1265'),
                array('昆山','SUZD1265'),
                array('苏州','NJB195'),
                array('无锡','NJB195'),
                array('昆山','NJB195'),
                array('天津','TJE1748'),
                array('天津','TJD969'),
                array('郑州','ZZF3222'),
                array('佛山','GZE1839'),
                array('东莞','GZE1840'),
                array('武汉','WHC519'),
                array('南昌','NCF3066'),
                array('大连','DLF3348'),
                array('大连','TJE1748'),
                array('济南','JND662'),
                array('沈阳','SYF4390'),
                array('沈阳','TJE1748'),
                array('长春','CCE2531'),
                array('长春','TJE1748'),
                array('长沙','CSD1163'),
                array('长沙','GZE1840'),
                array('厦门','XMF3977'),
                array('西安','XAD1199'),
                array('昆明','KME2121'),
                array('福州','FZF4161'),
                array('福州','GZE1840'),
                array('宁波','NBE1860'),
                array('太原','BJE2132'),
                array('太原','TJE1748'),

                );
    }
    /**
     * 财务
     */
    private function _data_fin(){
        return array(
                array('上海','SHS339'),
                array('上海','SHE2078'),
                array('北京','BJD680'),
                array('北京','BJE2325'),
                array('成都','CDD1204'),
                array('大连','DLF2795'),
                array('东莞','GZE1839'),
                array('佛山','GZE1839'),
                array('福州','FZF4161'),
                array('广州','GZB141'),
                array('杭州','HZE1585'),
                array('合肥','HFF3118'),
                array('济南','JND662'),
                array('昆明','KME2121'),
                array('南昌','CDE2120'),
                array('南京','NJC236'),
                array('宁波','NBE1860'),
                array('青岛','QDE1733'),
                array('三亚','SYAE2757'),
                array('厦门','XMF3977'),
                array('深圳','SZD695'),
                array('惠州','SZD695'),
                array('沈阳','SYF4390'),
                array('石家庄','SJZE2158'),
                array('苏州','SUZE2668'),
                array('无锡','SUZE2668'),
                array('昆山','SUZE2668'),
                array('太原','BJE2132'),
                array('天津','TJC367'),
                array('武汉','WHC519'),
                array('西安','XAD1275'),
                array('长春','CCE2531'),
                array('长沙','CSD1162'),
                array('郑州','ZZF3222'),
                array('重庆','CQC386'),
                );
    }
    /**
     * it
     */
    private function _data_it(){
        return array(
                array('全国','SHC563'),
                array('全国','SHD772'),
                array('全国','SHS141'),
                array('全国','SHS158'),
                array('全国','SHE2447'),
                );
    }
    /**
     * 图书管理
     */
    private function _data_book(){
        return array(
                array('上海','SHD901')
                );
    }
    private function _data_hrd(){
        return array(
                array('全国','SHA018')
                );
    }
}